Passing Session[] and Request[] to Methods in C#

Posted by NYARROW on Stack Overflow See other posts from Stack Overflow or by NYARROW
Published on 2010-04-13T23:28:46Z Indexed on 2010/04/13 23:33 UTC
Read the original article Hit count: 144

Filed under:
|

In C#, How do you pass the Session[] and Request[] objects to a method?

I would like to use a method to parse out Session and Request paramaters for a .aspx page to reduce the size of my Page_Load method. I am passing quite a few variables, and need to support both POSTand GET methods. For most calls, not all variables are present, so I have to test every variable multiple ways, and the code gets long...

This is what I am trying to do, but I can't seem to properly identify the Session and Request paramaters (this code will not compile, because the arrays are indexed by number)

static string getParam(
    System.Web.SessionState.HttpSessionState[] Session,
    System.Web.HttpRequest[] Request,
    string id)
{
    string rslt = "";

    try
    {
        rslt = Session[id].ToString();
    }
    catch
    {
        try
        {
            rslt = Request[id].ToString();
        }
        catch { }
    }

    return rslt;
}

From Page_Load, I want to call this method as follows to retrieve the "MODE" paramater:

string rslt;
rslt = getParam(Session, Request, "MODE");

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about aspx